<?php

declare(strict_types=1);

/** oss上传文件的使用
 * User：Abell
 * Date：2020/5/8
 */

namespace App\Controller\Abell;

use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Xxh\FileStore\Service\FileStoreAbstract;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
/**
 * @Controller()
 */
class FileController extends AbstractController
{
    /**
     * @Inject
     * @var FileStoreAbstract
     */
    private $aefile;

    /**
     * 上传图片返回oss访问路径
     *
     * @RequestMapping(path="/abell/file/uploadfile", methods="post")
     */
    public function uploadfile(){

        $apath = $this->aefile->store(
            $this->request->file('img')
        );
        var_dump($apath);
        // tem/storage/946b9eb8c2752273870972225ae5cfd5.jpg
        // 这个存储到数据库里面 上传重复的图片会直接返回之前的图片结果

        return $this->aefile->url($apath);
        // https://fasion.oss-cn-hangzhou.aliyuncs.com/tem/storage/946b9eb8c2752273870972225ae5cfd5.jpg?OSSAccessKeyId=LTAIKqa4XUOwajiT&Expires=1588907765&Signature=HxQwEWR1sjesITAoN4xWJHVu8U0%3D
        // https://fasion.oss-cn-hangzhou.aliyuncs.com/tem/storage/946b9eb8c2752273870972225ae5cfd5.jpg?OSSAccessKeyId=LTAIKqa4XUOwajiT&Expires=1588909127&Signature=LpODxMZbjAbzGfi%2BNxsY%2FMfm5f0%3D
    }

    /**
     * 写入文件返回oss访问路径
     *
     * @RequestMapping(path="/abell/file/putfile", methods="post,get")
     */
    public function putfile(){
        $apath = $this->aefile->put('1.txt','hello oss');
        return $this->aefile->url($apath);
        // https://fasion.oss-cn-hangzhou.aliyuncs.com/tem/storage/1.txt?OSSAccessKeyId=LTAIKqa4XUOwajiT&Expires=1588908131&Signature=RxusHTNQ1BF7t%2F0YaShQU6DqLvg%3D
    }

    /**
     * 显示调用oss资源
     *
     * @RequestMapping(path="/abell/file/showfile", methods="post,get")
     */
    public function showfile(RequestInterface $request){
        if($request->has('path')){
            return ($this->aefile->fileExists($request->input('path'))) ? $this->aefile->url($request->input('path')) : '文件不存在';
        }else{return '参数错误';}
    }



}

